import random
from datetime import datetime

# 初始化数据
money = 3000
goods = {"鞋子": 100, "衣服": 200, "帽子": 50, "空调": 3000}
shopping_cart = []

# 优惠券系统
coupons = {
    "鞋子": 0.9,
    "衣服": 0.95,
    "帽子": 0.9,
    "空调": random.choice([0.9, 0.8, 0.7, 0.6]),
    "全场通用": random.choice([0.95, 0.9, 0.85])
}

# 随机抽取一张优惠券
print("今日福利，10张优惠券抽奖活动，输入 “抽取” 可获得一张随机商品优惠券！！！")
if str(input()) == "抽取":
    coupon_name, discount = random.choice(list(coupons.items()))
    print(f"恭喜抽到【{coupon_name}】{int(discount * 10)}折优惠券！")

# 购物主循环
print("\n请问您要购买什么商品？（输入商品名称）")
print("输入R/r 退货，输入Q/q 退出并打印购物小条")

while True:
    user_input = input().strip()

    # 处理退出
    if user_input.upper() == "Q":
        break

    # 处理退货
    if user_input.upper() == "R":
        if not shopping_cart:
            print("购物车为空，无法退货！")
            continue

        print("您的购物车：", shopping_cart)
        refund_item = input("请输入要退货的商品名称：").strip()

        if refund_item in shopping_cart:
            # 计算退款金额（考虑优惠券）
            base_price = goods[refund_item]
            if refund_item == coupon_name:
                refund_amount = round(base_price * discount, 2)
            elif coupon_name == "全场通用":
                refund_amount = round(base_price * discount, 2)
            else:
                refund_amount = base_price

            money += refund_amount
            shopping_cart.remove(refund_item)
            print(f"【{refund_item}】退货成功！已退款{refund_amount}元，当前余额{money}元")
        else:
            print("购物车中没有该商品！")
        continue

    # 处理购买
    if user_input in goods:
        base_price = goods[user_input]

        # 计算优惠后价格
        if user_input == coupon_name:
            final_price = round(base_price * discount, 2)
        elif coupon_name == "全场通用":
            final_price = round(base_price * discount, 2)
        else:
            final_price = base_price

        if money >= final_price:
            money -= final_price
            shopping_cart.append(user_input)
            print(f"购买成功！【{user_input}】原价{base_price}元，优惠后{final_price}元")
            print(f"当前卡余额：{money}元")
        else:
            print(f"余额不足！【{user_input}】需要{final_price}元，您的余额只有{money}元")
    else:
        print("没有这个商品，请重新输入！")

# 打印购物小条
if shopping_cart:
    print("\n" + "=" * 30 + " 购物小条 " + "=" * 30)
    print(f"购物时间：{datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
    print(f"优惠券：【{coupon_name}】{int(discount * 10)}折")
    print(f"购物数量：{len(shopping_cart)}件商品")

    total_original = 0
    total_discount = 0

    print("\n商品明细：")
    for item in shopping_cart:
        price = goods[item]
        if item == coupon_name or coupon_name == "全场通用":
            discounted = round(price * discount, 2)
        else:
            discounted = price

        print(f"- {item}: 原价{price}元  优惠后{discounted}元")
        total_original += price
        total_discount += discounted

    print("\n" + "-" * 68)
    print(f"商品原价总计：{total_original}元")
    print(f"优惠后总计：{total_discount}元")
    print(f"节省金额：{total_original - total_discount}元")
    print(f"卡内余额：{money}元")
    print("=" * 70)
else:
    print("您没有购买任何商品，欢迎下次光临！")